<template>
  <el-dialog
    title="添加店铺"
    :visible="shopVisible"
    @update:visible="v=>$emit('update:shopVisible',false)"
  >
    <el-form>
      <el-form-item :label="'店铺名称:'+$route.name">
        <el-input v-model="shopForm.shopName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="是否推荐:">
        <el-radio v-model="shopForm.radio" label="1">推荐</el-radio>
        <el-radio v-model="shopForm.radio" label="2">不推荐</el-radio>
      </el-form-item>
      <el-form-item label="店铺类别名称:">
        <el-select v-model="shopForm.shopTypeId" placeholder="请选择">
          <el-option
            v-for="item in $store.state.shop.allshopTypeList"
            :key="item._id"
            :label="item.shopTypeName"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="店铺图片">
        <el-upload
          name="shopPic"
          :auto-upload="false"
          :on-success="success"
          :data="shopForm"
          ref="upload"
          :headers="{headerToken:$store.state.admin.token}"
          :limit="1"
          class="upload-demo"
          action="/elm/shopList"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:shopVisible',false)">取 消</el-button>
      <el-button type="primary" @click="$refs.upload.submit()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "add-shop",
  props: {
    shopVisible: {
      type: Boolean,
      default: false
    },
    shopTypeId: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      shopForm: {
        shopName: "",
        shopTypeId: "",
        radio: "1"
      }
    };
  },
  mounted() {
    // console.log(this.$route.name);
    this.$store.dispatch("allgetShopTypeList");
    if (this.shopTypeId) {
      this.shopForm.shopTypeId = this.shopTypeId;
    }
  },
  methods: {
    success(data) {
      if (data.ok === 1) {
        this.$store.dispatch("getshopList");
        //将提交框隐藏
        this.$emit("update:shopVisible", false);
        //添加成功过给出提示
        this.$message({
          message: "添加成功",
          type: "success"
        });
        //清除表单中长传成功的图片信息
        this.$refs.upload.clearFiles();
        //清除表单中输入框的内容
        this.shopForm.shopName = "";
        if (this.$route.name !== "shopList") {
          this.$router.history.push("/shopList");
        }
      } else {
        this.$message({
          showClose: true,
          message: data.msg,
          type: "error"
        });
      }
    }
  }
};
</script>

